﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Diagnostics.CodeAnalysis;
using System.Web.Mvc;

namespace Bzure.Mvc.Views
{
    [FileLevelControlBuilder(typeof(BzureViewPageControlBuilder))]
    public class BzureViewPage : System.Web.Mvc.ViewPage, IAreaMaster
    {
        string _area;
        public string AreaName
        {
            get { return _area; }
            set { _area = value; SetMasterPageFile(); }
        }
        string _master;
        public string MasterPath
        {
            get
            {
                return _master;
            }
            set
            {
                _master = value; SetMasterPageFile();
            }
        }
        [Obsolete("MasterPageFile属性已经过时，请使用Master属性设置母板相对路径", true)]
        public new string MasterPageFile
        {
            get;
            set;
        }
        void SetMasterPageFile()
        {
            if (!string.IsNullOrEmpty(_master))
                base.MasterPageFile = ThemeHelper.CreateThemeViewPath(Actor.Me.ThemeName, _area, _master);
        }
    }

    [FileLevelControlBuilder(typeof(BzureViewPageControlBuilder))]
    public class BzureViewPage<TModel> : BzureViewPage       
    {
        // code copied from source of ViewPage<T>

        private ViewDataDictionary<TModel> _viewData;

        public new AjaxHelper<TModel> Ajax
        {
            get;
            set;
        }

        public new HtmlHelper<TModel> Html
        {
            get;
            set;
        }

        public new TModel Model
        {
            get
            {
                return ViewData.Model;
            }
        }

        [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public new ViewDataDictionary<TModel> ViewData
        {
            get
            {
                if (_viewData == null)
                {
                    SetViewData(new ViewDataDictionary<TModel>());
                }
                return _viewData;
            }
            set
            {
                SetViewData(value);
            }
        }

        public override void InitHelpers()
        {
            base.InitHelpers();

            Ajax = new AjaxHelper<TModel>(ViewContext, this);
            Html = new HtmlHelper<TModel>(ViewContext, this);
        }

        protected override void SetViewData(ViewDataDictionary viewData)
        {
            _viewData = new ViewDataDictionary<TModel>(viewData);

            base.SetViewData(_viewData);
        }

    }
}